package com.atguigu.gmall.realtime.function;

import com.atguigu.gmall.realtime.utils.KeywordUtils;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.functions.TableFunction;
import org.apache.flink.types.Row;

import java.util.List;

/**
 * 用户自定义表函数实现分词功能（UDTF）
 *
 * @author lvbingbing
 * @date 2022-07-06 15:25
 */
@FunctionHint(output = @DataTypeHint("ROW<word STRING>"))
public class KeywordAnalyzeFunction extends TableFunction<Row> {

    @SuppressWarnings("unused")
    public void eval(String str) {
        List<String> list = KeywordUtils.analyze(str);
        for (String s : list) {
            // use collect(...) to emit a row
            collect(Row.of(s));
        }
    }
}